<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_V2_Index extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Index
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Index
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('index', $this->request_params);

        // 服务
        $this->service = new Service_Api_Index();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 首页导航
    public function action_index_nav()
    {
        try {
            $this->validation->index_nav()->check();
            $cache_key = Util_CacheKey::api_v2('index_nav', Util_CacheKey::make_arguments($this->request_params));
            $data      = Model_Api_V2_Common::cache('get', $cache_key);
            if (!$data) {
                $data = $this->service->index_nav($this->request_params);
                $expire_time = rand(10, 30);
                Model_Api_V2_Common::cache('set', $cache_key, $data, $expire_time);
            }
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 底部菜单
    public function action_foot_menu()
    {
        try {
            $this->validation->foot_menu()->check();

            $cache_key = Util_CacheKey::api_v2('foot_menu', Util_CacheKey::make_arguments($this->request_params));
            $data      = Model_Api_V2_Common::cache('get', $cache_key);
            if (!$data) {
                $data = $this->service->foot_menu($this->request_params);
                $expire_time = rand(10, 30);
                Model_Api_V2_Common::cache('set', $cache_key, $data, $expire_time);
            }
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 热门攻略
    public function action_hot_raiders()
    {
        try {
            $this->validation->hot_raiders()->check();

            $this->result['data'] = $this->service->hot_raiders($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 热门游记
    public function action_hot_notes()
    {
        try {
            $this->validation->hot_notes()->check();

            $this->result['data'] = $this->service->hot_notes($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 热门专题
    public function action_hot_zt()
    {
        try {
            $this->validation->hot_zt()->check();

            $this->result['data'] = $this->service->hot_zt($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 热门产品导航
    public function action_hot_product_nav()
    {
        try {
            $this->validation->hot_product_nav()->check();

            $this->result['data'] = $this->service->hot_product_nav($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 热门产品
    public function action_hot_product()
    {
        try {
            $this->validation->hot_product()->check();

            $this->result['data'] = $this->service->hot_product($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 实时播报 实际为全站最新订单
    public function action_notice()
    {
        try {
            $this->validation->notice()->check();

            $this->result['data'] = $this->service->notice($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 是否开启分销
    public function action_is_show_distribute()
    {
        try {
            $this->validation->is_show_distribute()->check();

            $this->result['data']['is_show'] = $this->service->is_show_distribute($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => true, 'msg' => $e->getMessage(), 'data'=> array('is_show'=> false));
        }
    }

    // 文旅首页热门推荐
    public function action_hot_wenlv_index()
    {
        try {
            $this->validation->hot_wenlv_index()->check();

            $this->result['data'] = $this->service->hot_wenlv_index($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => true, 'msg' => $e->getMessage(), 'data'=> array('is_show'=> false));
        }
    }

    // 文旅模板发现页-导航
    public function action_wenlv_nav()
    {
        try {
            $this->result['data'] = $this->service->wenlv_nav($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => true, 'msg' => $e->getMessage(), 'data'=> array('is_show'=> false));
        }
    }

    // 文旅模板发现页-列表
    public function action_wenlv_list()
    {
        try {
            $this->validation->wenlv_list()->check();

            $this->result['data'] = $this->service->wenlv_list($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => true, 'msg' => $e->getMessage(), 'data'=> array('is_show'=> false));
        }
    }
}